package 栈;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author tmh
 * @date 2024/9/11 22:47
 * @description
 */
public class T20有效的括号 {
    public boolean isValid(String s) {
        Deque<Character> deque = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            if (c == '(') {
                deque.push(')');
            } else if (c == '{') {
                deque.push('}');
            } else if (c == '[') {
                deque.push(']');
                //这个情况要注意下
            } else if (deque.isEmpty() || deque.peek() != c) {
                return false;
            }else {
                deque.pop();
            }
        }
        return deque.isEmpty();
    }
}
